package com.fita.wechat.handler.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.demo2do.core.entity.exception.BusinessException;
import com.fita.entity.data.Location;
import com.fita.entity.order.Order;
import com.fita.entity.order.OrderSpecialEquipment;
import com.fita.entity.order.OrderVehicleFee;
import com.fita.entity.set.Set;
import com.fita.wechat.handler.PDFHandler;
import com.fita.wechat.handler.support.PDFTemplate;
import com.itextpdf.text.DocumentException;

/**
 * 
 * @author dan
 *
 */
@Component("pdfHandler")
public class PDFHandlerImpl implements PDFHandler{
    
    private static final Logger logger = LoggerFactory.getLogger(PDFHandlerImpl.class);
    
    @Value("#{config['cloud.path']}")
    private String cloudpath;
    
    @Value("#{cloud['cloud.host']}")
    private String cloudhost;
    
    @Value("#{config['font.path']}")
    private String fontPath;
    
    @Autowired
    private VelocityEngine velocityEngine;

    /*
     * (non-Javadoc)
     * @see com.fita.wechat.handler.PDFHandler#saveOrderConfirm(com.fita.entity.order.Order, com.fita.entity.data.Location, com.fita.entity.data.Location, com.fita.entity.set.Set, java.util.List, java.util.List, java.util.List, java.lang.String, java.lang.String)
     */
    public String saveOrderConfirm(Order order, Location pickupLocation, Location returnLocation, Set set,
            List<OrderVehicleFee> orderIncludeFees, List<OrderVehicleFee> orderUnIncludeFees,
            List<OrderSpecialEquipment> equipments, String vehicleSize, String vehicleCategory) {
        try {
            
            logger.info("start save delegate report");
            
            Map<String, Object> model = new HashMap<String, Object>();
            
            model.put("order", order);
            model.put("pickupLocation", pickupLocation);
            model.put("returnLocation", returnLocation);
            model.put("set", set);
            model.put("orderIncludeFees", orderIncludeFees);
            model.put("orderUnIncludeFees", orderUnIncludeFees);
            model.put("equipments", equipments);
            model.put("vehicleSize", vehicleSize);
            model.put("vehicleCategory", vehicleCategory);
                
            logger.info("start save delegate report data alerday");
            
            String fileName = order.getAlamoCode() + ".pdf";
            PDFTemplate file = new PDFTemplate(fileName).initialize(velocityEngine, model, "content/order-confirm.vm", fontPath);
            
            logger.info("start save delegate report file generated");
            
            String url = file.toPDF(this.cloudpath, "order-confirm-report", this.cloudhost);
            
            logger.info("start save delegate report file url get [{}]", url);
            
            return url;
            
        } catch (IOException e) {
            logger.error(" savaPDF() - error is [{}]", e.getMessage());
            throw new BusinessException("save pdf failed");
        } catch (DocumentException e) {
            logger.error(" savaPDF() - error is [{}]", e.getMessage());
            throw new BusinessException("save pdf failed");
        }
    }

}
